package zx.x.modules.yshapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.R;
import zx.x.modules.app.entity.UserEntity;
import zx.x.modules.app.service.UserService;
import zx.x.modules.yshapp.dao.RaffleAppDao;
import zx.x.modules.yshapp.dao.RaffleSignUpUserAppDao;
import zx.x.modules.yshapp.entity.CommunityEntity;
import zx.x.modules.yshapp.entity.GoodsEntity;
import zx.x.modules.yshapp.entity.RaffleEntity;
import zx.x.modules.yshapp.entity.RaffleSignUpUserEntity;
import zx.x.modules.yshapp.service.CommunityService;
import zx.x.modules.yshapp.service.GoodsService;
import zx.x.modules.yshapp.service.RaffleService;
import zx.x.modules.yshapp.service.RaffleSignUpUserService;
import zx.x.modules.yshapp.vo.RaffleVo;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 抽奖
 */
@RestController
@RequestMapping("yshapp/raffle")
public class RaffleAppController {
    @Autowired
    private RaffleService raffleService;
    @Autowired
    private RaffleSignUpUserService raffleSignUpUserService;
    @Autowired
    private RaffleSignUpUserAppDao raffleSignUpUserAppDao;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private RaffleAppDao raffleAppDao;

    /**
     *参加抽奖
     */
    @RequestMapping("/signUp")
    public R signUp(RaffleSignUpUserEntity raffleSignUpUserEntity){

        if(raffleSignUpUserAppDao.selectOne(new QueryWrapper<RaffleSignUpUserEntity>()
                .eq("raffle_id",raffleSignUpUserEntity.getRaffleId())
                .eq("user_id",raffleSignUpUserEntity.getUserId())) != null){
            return R.error("您已报名");
        }
        RaffleEntity raffleEntity = raffleService.getOne(new QueryWrapper<RaffleEntity>()
                .eq("id",raffleSignUpUserEntity.getRaffleId()));


        if(raffleEntity.getIsOpen() == 1){
            return R.error("已开奖");
        }
        if(new Date().after(raffleEntity.getEndTime())){
            return R.error("报名时间已过");
        }

        raffleSignUpUserEntity.setCjTime(new Date());
        raffleSignUpUserEntity.setIsWin(1);
        raffleSignUpUserEntity.setIsReceive(1);
        raffleSignUpUserAppDao.insert(raffleSignUpUserEntity);

        return R.ok();
    }

    @RequestMapping("/signUpHouse")
    public R signUpHouse(Integer userId){
        UserEntity user = userService.getById(userId);
        Integer communityId = user.getCommunityId();

        RaffleVo raffleVo = new RaffleVo();
        raffleVo.setIsWin(0);//未中奖
        //在线人数没想到怎么写
//        raffleVo.setOnlineNum(456);
        //该社区待开奖的抽奖活动数
        int wkjCount = raffleSignUpUserAppDao.getCount(communityId,user.getType());//能参加几个抽奖活动
        RaffleEntity entity;
        if(wkjCount>0){//未开奖的
            entity = raffleService.getOne(new QueryWrapper<RaffleEntity>()
                    .eq("community_id",communityId)
                    .eq("type",user.getType())
                    .eq("is_open",0)
                    .orderBy(true,false,"open_time")
                    .last("limit 1"));
        }else {//已开奖的
            entity = raffleService.getOne(new QueryWrapper<RaffleEntity>()
                    .eq("community_id",communityId)
                    .eq("type",user.getType())
                    .eq("is_open",1)
                    .orderBy(true,false,"open_time")
                    .last("limit 1"));
            if(entity != null){
                //中奖名单
                List<Map<String,Object>> mapList = raffleSignUpUserAppDao.getWinNameList(entity.getId());
                raffleVo.setWinList(mapList);
                for (Map<String,Object> map : mapList){
                    if(map.get("user_id").equals(userId)){
                        raffleVo.setIsWin(1);
                    }
                }
            }
        }

        if(entity == null){
            return R.error("暂无抽奖活动");
        }
        int signUpNum = raffleSignUpUserAppDao.selectCount(new QueryWrapper<RaffleSignUpUserEntity>().eq("raffle_id",entity.getId()));
        raffleVo.setSignUpNum(signUpNum);

        //抽奖id
        raffleVo.setRaffleId(entity.getId());

        //第几期
        raffleVo.setPeriod(entity.getPeriod());

        //是否开奖
        raffleVo.setIsOpen(entity.getIsOpen());

        //奖品数量
        raffleVo.setPrizeNum(entity.getPrizeNum());

        //开奖时间
        raffleVo.setOpenTime(entity.getOpenTime());

        //奖品名称
        GoodsEntity goodsEntity = goodsService.getOne(new QueryWrapper<GoodsEntity>().eq("id",entity.getGoodsId()));
        raffleVo.setPrizeName(goodsEntity.getName());

        //是否参与
        raffleVo.setIsSignUp(raffleSignUpUserAppDao
                .selectCount(new QueryWrapper<RaffleSignUpUserEntity>()
                        .eq("user_id",userId).eq("raffle_id",entity.getId())));

        //奖品图片
        raffleVo.setGoodsPic(goodsEntity.getCoverPic());

        //社区名字
        CommunityEntity communityEntity = communityService.getOne(new QueryWrapper<CommunityEntity>().eq("id",communityId));
        raffleVo.setCommunityName(communityEntity.getCommunityName());

        //是否有没领奖的奖品：0有1无
        Map<String, Object> haveReceive = raffleAppDao.getReceive(userId);
        if(haveReceive == null){//
            raffleVo.setHaveReceive(1);
        }else {
            raffleVo.setHaveReceive(0);
            //领取奖品名称
            raffleVo.setReceiveGoodsName((String) haveReceive.get("name"));
            //报名表id
            raffleVo.setRsuuId((Integer) haveReceive.get("rsuuId"));
        }

        return R.ok().put("raffleVo",raffleVo);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = raffleService.queryPage(params);
        return R.ok().put("page", page);
    }
    @RequestMapping("/detail")
    public R detail(Integer id, Integer userId) {
        RaffleVo raffleVo = new RaffleVo();
        RaffleEntity raffleEntity =raffleService.getById(id);
        if(raffleEntity != null){
            BeanUtils.copyProperties(raffleEntity,raffleVo);
            CommunityEntity communityEntity =communityService.getById(raffleEntity.getCommunityId());
            raffleVo.setCommunityName(communityEntity.getCommunityName());
            RaffleSignUpUserEntity raffleSignUpUserEntity = raffleSignUpUserService
                    .getOne(new QueryWrapper<RaffleSignUpUserEntity>()
                            .eq("raffle_id",raffleEntity.getId())
                            .eq("user_id",userId));
            if(raffleSignUpUserEntity != null){
                raffleVo.setIsSignUp(0);
                    raffleVo.setIsReceive(raffleSignUpUserEntity.getIsReceive());
                raffleVo.setIsWin(raffleSignUpUserEntity.getIsWin());
                raffleVo.setReceiveTime(raffleSignUpUserEntity.getReceiveTime());
            }else{
                raffleVo.setIsSignUp(1);
                raffleVo.setIsReceive(1);
                raffleVo.setIsWin(1);
            }
            GoodsEntity goodsEntity =goodsService.getById(raffleEntity.getGoodsId());
            raffleVo.setPrizeName(goodsEntity.getName());
            int signUpNum =raffleSignUpUserService.count(new QueryWrapper<RaffleSignUpUserEntity>()
                    .eq("raffle_id",raffleEntity.getId()));
            raffleVo.setSignUpNum(signUpNum);
            raffleVo.setGoodsPic(goodsEntity.getCoverPic());

        }
        return R.ok().put("raffleVo", raffleVo);
    }


}
